<template>
  <div class="conMain" >
    <el-card  class="box-card" >
      <div slot="header" class="topfix">
          <el-form   :inline="true"  :model="params" size="small" class="goodsForm"  label-width="100px">
              <el-form-item label="ID">
                  <el-input v-model="params.s_id"  placeholder="请输入ID" />
              </el-form-item>
              <el-form-item label="标题">
                  <el-input v-model="params.s_title"  placeholder="请输入用户名" />
              </el-form-item>
              <el-form-item label="分类">
                  <el-cascader v-model="params.s_cate_id" :options="catelist" :show-all-levels="false" :props="{ checkStrictly: true,emitPath:false }" clearable></el-cascader>
              </el-form-item>

              <el-form-item label="状态">
                  <el-select v-model="params.s_status"  placeholder="请选择">
                      <el-option  :label="item.txt" :value="item.val" v-for="(item,index) in statuslist" />
                  </el-select>
              </el-form-item>
              <el-form-item label=" ">
              <el-button type="primary"  size="small" class="searchBtn" @click="handleSearch" icon='el-icon-search'>搜索</el-button>
              <el-button type="info"  @click="handleRet" plain>重置</el-button>
              </el-form-item>
          </el-form>
          <div style="text-align:right;">
            <router-link to="/cms/article_add">
              <el-button v-has="'cms/article/add'"  type="primary" size="small" class="addBtn"  plain><i class="el-icon-plus" /> 添加文章</el-button>
            </router-link>
          </div>
      </div>
      <el-table v-loading="loading"  :data="list" style="width: 100%" :empty-text='emtypetxt'>
          <el-table-column prop="id" label="ID" align="center" width="100" />
          <el-table-column prop="title" label="标题" align="center" min-width="150" />
          <el-table-column prop="cate_name" label="分类" align="center" />
          <el-table-column prop="image" label="主图"  align="center" >
            <template slot-scope="scope">
              <el-image
                  style="width:40px;height:40px"
                  :src="scope.row.image"
                  :preview-src-list="[scope.row.image]"
              >
              <div slot="error" class="image-slot"></div>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="is_rec" label="推荐"  align="center" >
            <template slot-scope="scope">
                <el-switch
                    v-model="scope.row.is_rec"
                    active-color="#13ce66"
                    :active-value= "1"
                    :inactive-value= "0"
                    @change="changeRec(scope.row.id,$event)"

                >
                </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="see_num" label="浏览量" align="center" />
          <el-table-column prop="weigh" label="排序" align="center" />
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 1" type="success" size="mini" disable-transitions>正常</el-tag>
              <el-tag v-if="scope.row.status === 0" type="info" size="mini" disable-transitions>隐藏</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createtime" label="创建时间" align="center" width="200" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope" >
                <el-link v-has="'cms/article/edit'" :underline="false" type="primary" title='编辑' @click="edit(scope.row.id)">编辑</el-link>
                <el-link v-has="'cms/article/del'" :underline="false" type="danger" title='删除' @click="del(scope.row.id)" class='ml10'>删除</el-link>
            </template>
          </el-table-column>
      </el-table>
      <div class="pagination">
          <el-pagination
            :current-page="params.page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="params.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
      </div>
    </el-card>
  </div>
</template>

<script>

import { article_index,article_add,article_edit,article_del,article_multi,cascader_list } from  '@/api/cms'


export default {
  data() {
    return {
      total:0,
      loading:true,
      emtypetxt:'',
      list: [],
      catelist:[],//文章分类
      statuslist:[{val:'1',txt:'正常'},{val:'0',txt:'隐藏'}],
      params: {
        s_id:'',
        s_title:'',
        s_cate_id:'',
        s_status:'',
        page: 1,
        pagesize:10,
      },
    }
  },
  created() {
    cascader_list().then(res => {
      this.catelist=res.data
    })
    this.getList()
  },
  methods: {
    getList() {
      this.emtypetxt='加载中...';
      this.loading=true;
      article_index(this.params).then(res => {
        this.list=res.data.list
        this.total=res.data.total
        this.loading = false
        if(this.list.length==0){
          this.emtypetxt='暂无数据'
        }
      })
    },
    handleRet(){
      this.params={
        s_id:'',
        s_title:'',
        s_cate_id:'',
        s_status:'',
        page: 1,
      },
      this.list=[];
      this.getList()
    },
    handleSearch(){
      this.list=[];
      this.params.page=1;
      this.getList()
    },
    //分页
    handleSizeChange(val) {
        this.params.pagesize = val
        this.getList()
    },
    handleCurrentChange(val) {
        this.params.page = val
        this.getList()
    },
    //编辑
    edit(id){
      this.$router.push('/cms/article_add?id='+id)
    },
    del(id) {
      let _this=this;
      _this.$modalSure().then(() => {
        article_del({ids:id}).then(res => {
          _this.$message.success(res.msg)
          _this.getList()
        })
      })
    },
    // 切换状态
    changeRec(id,val){
      var pp="is_rec="+val;
      article_multi({ids:id,params:pp}).then(res => {
          this.$message.success(res.msg)
      })
    },

    resetsub(){
      this.sub.id=0
      this.sub.name=''
      this.sub.cardnum=''
      this.sub.nation=''
      this.sub.sex='1'
      this.sub.card_province=''
      this.sub.card_city=''
      this.sub.card_address=''
      this.sub.join_time=''
      this.sub.mobile=''
      this.sub.edu=''
      this.sub.status='1'

    },




  },

}
</script>

<style lang="scss" scoped>


</style>
